import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

public class StaticResourceUtil {
    /**
     * 获取请求页面的地址
     * @param path
     * @return
     */
    public static String getAbsolutePath(String path){
        String absolutePath = StaticResourceUtil.class.getResource("/").getPath();

        return  absolutePath.replaceAll("\\\\","/") + path;

    }

    /**
     * 文件相应客户处
     * @param fileInputStream
     * @param outputStream
     * @throws IOException
     */
    public static void outputStaticResource(FileInputStream fileInputStream, OutputStream outputStream) throws IOException {
        int count = 0;
        while (count ==0){
            count  = fileInputStream.available();
        }
        int resourceSize = count;
        outputStream.write(HttpProcotoUtil.getHeader200(resourceSize).getBytes());
        long written = 0;
        int byteSize = 1024;
        byte[] bytes = new byte[byteSize];
        while (written<byteSize){
            if(byteSize + written>resourceSize){    //说明文件大小不足1024byte
                byteSize = (int) (resourceSize -written);//剩余的文件长度
                bytes = new byte[byteSize];
            }
            fileInputStream.read(bytes);
            outputStream.write(bytes);
            outputStream.flush();
            written += byteSize;
        }

    }
}
